/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.PaginationUtil;
import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.FlatListingForm;
import com.ntb.entities.Flats;
import com.ntb.models.FlatsDAO;
import com.ntb.models.impl.FlatsDAOImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class FlatListingAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

        FlatListingForm flatListingForm = (FlatListingForm) form;

        FlatsDAO flatDAO = new FlatsDAOImpl();

        String action = flatListingForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        boolean isHaveAction = true;
        if (("delete").equalsIgnoreCase(action)) {
            if (flatDAO.delete(flatListingForm.getFlatID()) == 0) {
                errorMessages.add("There was an error deleting this flat. You must delete all sales associated with this flat before deleting the flat.");
            } else {
                flatDAO.delete(flatListingForm.getFlatID());
                successMessages.add("1 flats out of 1 successfully deleted.");
            }
        } else if (("deleteFlats").equalsIgnoreCase(action)) {
            int[] flatIDs = flatListingForm.getFlatIDs();
            int count = 0;

            int length = 0;

            try {
                length = flatIDs.length;
            } catch (Exception ex) {
            }

            if (length > 0) {
                for (int flatID : flatIDs) {
                    if (flatDAO.delete(flatID) == 0) {
                        errorMessages.add("There was an error deleting this flat. You must delete all flats associated with this flat before deleting the flat.");
                    } else {
                        flatDAO.delete(flatID);
                        count++;
                    }
                }
                successMessages.add(count + " flats out of " + length + " successfully deleted.");
            }
        } else {
            isHaveAction = false;
        }

        if (isHaveAction) {
            request.setAttribute("errorMessages", errorMessages);
            request.setAttribute("successMessages", successMessages);
        }

        String baseUrl = request.getRequestURI() + "?";

        String flatId = null;
        String flatName = null;
        String buildingId = null;
        String buildingName = null;
        int floorFrom = -1;
        int floorTo = -1;
        int areaFrom = -1;
        int areaTo = -1;

        int page = -1;

        flatId = flatListingForm.getId();
        flatId = "".equals(flatId) ? null : flatId;
        if (flatId != null) {
            baseUrl += "id=" + flatId + "&amp;";
        }

        flatName = flatListingForm.getName();
        flatName = "".equals(flatName) ? null : flatName;
        if (flatName != null) {
            baseUrl += "name=" + flatName + "&amp;";
        }

        buildingId = flatListingForm.getBuilding();
        buildingId = "".equals(buildingId) ? null : buildingId;
        if (buildingId != null) {
            buildingId += "building=" + buildingId + "&amp;";
        }

        buildingName = flatListingForm.getBuildingN();
        buildingName = "".equals(buildingName) ? null : buildingName;
        if (buildingName != null) {
            buildingName += "buildingN=" + buildingName + "&amp;";
        }

        try {
            floorFrom = Integer.parseInt(flatListingForm.getFloorF());
        } catch (Exception e) {
        }
        if (floorFrom > 0) {
            baseUrl += "floorF=" + floorFrom + "&amp;";
        } else {
            floorFrom = -1;
        }

        try {
            floorTo = Integer.parseInt(flatListingForm.getFloorT());
        } catch (Exception e) {
        }
        if (floorTo > 0) {
            baseUrl += "floorT=" + floorTo + "&amp;";
        } else {
            floorTo = -1;
        }

        try {
            areaFrom = Integer.parseInt(flatListingForm.getAreaF());
        } catch (Exception e) {
        }
        if (areaFrom > 0) {
            baseUrl += "areaF=" + areaFrom + "&amp;";
        } else {
            areaFrom = -1;
        }

        try {
            areaTo = Integer.parseInt(flatListingForm.getAreaT());
        } catch (Exception e) {
        }
        if (areaTo > 0) {
            baseUrl += "areaT=" + areaTo + "&amp;";
        } else {
            areaTo = -1;
        }

        try {
            int i = Integer.parseInt(request.getParameter("page"));
            page = i > 1 ? i : 1;
        } catch (Exception ex) {
        }

        Iterator<Flats> flatList = flatDAO.getList(flatId, flatName, buildingId, buildingName, floorFrom, floorTo, areaFrom, areaTo, page);
        request.setAttribute("flatList", flatList);

        PaginationUtil paginationUtil = new PaginationUtil();
        paginationUtil.setBaseUrl(baseUrl);
        paginationUtil.setNumPages(flatDAO.getPageCount(flatId, flatName, buildingId, buildingName, floorFrom, floorTo, areaFrom, areaTo));
        paginationUtil.setCurPage(page);
        request.setAttribute("pagination", paginationUtil.createLinks());

        return mapping.getInputForward();
    }
}
